package com.yx.gsyd.controller.app;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.excel.util.MapUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yx.gsyd.comm.DictionaryEnum;
import com.yx.gsyd.entity.SysManager;
import com.yx.gsyd.entity.search.TaskListSearch;
import com.yx.gsyd.entity.view.MainCountView;
import com.yx.gsyd.satoken.StpUserUtil;
import com.yx.gsyd.service.IMainCountService;
import com.yx.gsyd.service.ISysManagerRoleService;
import com.yx.gsyd.service.ISysManagerService;
import com.yx.gsyd.service.ISysRoleMenuService;
import com.yx.gsyd.util.RandomValidateCodeUtil;
import com.yx.gsyd.util.ResultView;
import com.yx.gsyd.util.RsaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Validated
@RestController
@RequestMapping("/api/front/login")
public class AppAdminLoginController {

    @Autowired
    private ISysManagerRoleService iHjManagerRoleService;
    @Autowired
    private ISysRoleMenuService iHjRoleMenuService;
    @Autowired
    private ISysManagerService iHjManagerService;
    @Autowired
    private IMainCountService iMainCountService;

    @Autowired
    private ISysManagerService iSysManagerService;

    /**
     * 服务中心员工登录
     * @param account
     * @param pwd
     * @return
     */
    @PostMapping("/appLogin")
    public ResultView appLogin(@NotNull(message = "手机号不能为空") String account,
                              @NotNull(message = "密码不能为空") String pwd){
        //进行登录认证
        QueryWrapper<SysManager> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("m_account",account);
        queryWrapper.eq("is_del", DictionaryEnum.IS_DEL_N.getCode());
        SysManager hjManager=this.iHjManagerService.getOne(queryWrapper);
        if(hjManager==null){
            return ResultView.error("账号不存在！！！");
        }
        if(hjManager.getMStatus()==DictionaryEnum.IS_ENABLE_Y.getCode()){
            return ResultView.error("账号已被禁用！！！");
        }
        if(!RsaUtil.getRsaPwd(hjManager.getMPwd()).equals(pwd)){
            return ResultView.error("密码错误");
        }
        if(hjManager.getMType()==6||hjManager.getMType()==2){//如果是员工登录，需要区分只能是服务中心员工才可以登录
            if(hjManager.getMType()==6){
                SysManager parent=this.iHjManagerService.getById(hjManager.getParentManagerId());
                if(parent.getMType()!=2){
                    return ResultView.error("当前账户禁止在员工服务端登录");
                }
            }
        }else{
            return ResultView.error("当前账户禁止在员工服务端登录");
        }
        //进行授权注入sa-token权限框架
        StpUserUtil.login(hjManager.getManagerId());
        return ResultView.ok(StpUserUtil.getTokenValue());
    }


    public static void main(String[] args) {
        Integer mty=2;
        System.out.println(mty!=2);
    }

    /**
     * 获取当前用户信息
     */
    @GetMapping(value = "/getAccountInfo")
    public ResultView getAccountInfo() {
        Map<String, Object> map = MapUtils.newHashMap();
        String managerId= StpUserUtil.getLoginIdAsString();
        SysManager sysManager=this.iHjManagerService.getById(managerId);
        map.put("user",sysManager);
        map.put("parentType","");
        if(sysManager.getMType()==6){
            SysManager parent=this.iHjManagerService.getById(sysManager.getParentManagerId());
            map.put("parentType",parent.getMType());
            map.put("parentName",parent.getMName());
        }
        TaskListSearch obj=new TaskListSearch();
        if(sysManager.getMType()==1||sysManager.getMType()==3){
            System.out.println("全部权限");
            obj.setAdminId("all");
        }else{
            if(sysManager.getMType()==2){
                obj.setFwzxzrManagerId(sysManager.getManagerId());
            }
            if(sysManager.getMType()==4){
                obj.setJhtdzrManagerId(sysManager.getManagerId());
            }
            if(sysManager.getMType()==5){
                obj.setYhtdzrManagerId(sysManager.getManagerId());
            }
            if(sysManager.getMType()==6){
                SysManager parent=this.iSysManagerService.getById(sysManager.getParentManagerId());
                if(parent.getMType()==2){
                    obj.setFwzxygManagerId(sysManager.getManagerId());
                }
                if(parent.getMType()==4){
                    obj.setJhtdygManagerId(sysManager.getManagerId());
                }
                if(parent.getMType()==5){
                    obj.setYhtdygManagerId(sysManager.getManagerId());
                }
            }
        }
        MainCountView mainCountView=this.iMainCountService.getMainCount(obj);
        map.put("mainCountView",mainCountView);
        return ResultView.ok(map);
    }

    /**
     * 重置密码
     * @param mPwd
     * @return
     */
    @PostMapping("/restPwd")
    public ResultView restPwd(@NotNull(message = "旧密码不能为空") String mPwd,
                              @NotNull(message = "新密码不能为空") String newPwd,
                              @NotNull(message = "二次确认新密码不能为空") String aginPwd) {
        if(!newPwd.equals(aginPwd)){
            return ResultView.error("两次密码不一致");
        }
        return this.iSysManagerService.restAppPwd(mPwd,newPwd);
    }


}
